//
// Created by Administrator on 2025/3/13.
//

#include "word_query_data_dao.h"

extern void ui_word_query_main_screen_show(void);
extern void ui_word_query_main_screen_deinit(void);
extern void ui_word_query_with_keyboard_show();
extern void ui_word_query_with_keyboard_deinit();

ui_word_query_data_t ui_word_query_data = {0};

// 小写英文字母数组（字符串数组）
char *lowercase_letter_strings[] = {
        "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","空","-","'"
};
int lowercase_letter_strings_size = sizeof(lowercase_letter_strings)/sizeof(char *);

// 大写英文字母数组（字符串数组）
char *uppercase_letter_strings[] = {
        "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","空","-","'"
};
int uppercase_letter_strings_size = sizeof(uppercase_letter_strings)/sizeof(char *);

// 数字数组（字符串数组）
char *digit_strings[] = {
        "0","1","2","3","4","5","6","7","8","9"
};
int digit_strings_size = sizeof(digit_strings)/sizeof(char *);

// 特殊符号数组（字符串数组）
char *special_characters[] = {
        "!","@","#","¥","%","^","&","*","(",")","/","_","=","+","~","|","\\",
        "{","}","[","]",".","?","\"","'", "<",">",",",":",";"
};
int special_characters_size = sizeof(special_characters)/sizeof(char *);


bool get_keyboard_connect_status(){
    static bool is_keyboard_connected = false;
    is_keyboard_connected = !is_keyboard_connected;
    return is_keyboard_connected;
}

// 数据初始化
void ui_word_query_data_init(ui_word_query_data_t *data){
    LOG_INFO_WRAPPER("start");
    if(data){
        data->is_with_keyboard = get_keyboard_connect_status();
        data->letter_parent_size = 4;
        data->letter_parent_index = 0;
        data->letter_child_size = lowercase_letter_strings_size;
        data->letter_focus_index = 0;
        memset(data->input_word, 0, WORD_QUERY_MAX_WORD_LEN);
        data->input_word_len = 0;
        if(data->word_list){
            free(data->word_list);
            data->word_list = NULL;
        }
//        data->word_list = word_query_get_recently_word_list(&(data->word_list_size));
    }
}

word_query_word_entry_t *word_query_get_recently_word_list(int *size,int max_size) {
    static int count = 1;
//    char *word_list_label_text1[] = {"最近查询单词:"};
    char *word_list_label_text1[] = {"最近查询单词:","clean 打扫","surprise 惊奇","hungry 饥饿的",
                                     "thirsty 口渴的","picnic 野餐"};
//    char *word_list_label_text2[] = {"最近查询单词:","clean 打扫"};
    char *word_list_label_text2[] = {"最近查询单词:","clean 打扫","surprise 惊奇","hungry 饥饿的",
                                     "thirsty 口渴的","picnic 野餐"};
//    char *word_list_label_text[] = {"sh 虚","speak 说（某种语言）","surprise 惊奇","hungry 饥饿的",
//                                    "thirsty 口渴的","picnic 野餐"};
//    char *word_list_label_text[] = {"sh 虚","speak 说（某种语言）","surprise 惊奇","hungry 饥饿的",
//                                    "thirsty 口渴的"};
    char **word_lists[] = {word_list_label_text1, word_list_label_text2};
    int list_sizes[] = {
            sizeof(word_list_label_text1) / sizeof(char *),
            sizeof(word_list_label_text2) / sizeof(char *)
    };
    int list_count = sizeof(word_lists) / sizeof(word_lists[0]); // 总共有几个列表
    int index = count % list_count;  // 轮询索引
    char **selected_list = word_lists[index];
    *size = list_sizes[index];
    count++; // 更新 count，保证下次切换
    // 如果选中的列表为空，直接返回 NULL
    if (selected_list == NULL || *size == 0) {
        return NULL;
    }
    if(*size > max_size){
        *size = max_size;
    }

    // 分配结构体数组（连续存储）
    word_query_word_entry_t *word_array = (word_query_word_entry_t *)malloc((*size) * sizeof(word_query_word_entry_t));
    // 逐个复制单词
    for (int i = 0; i < *size; ++i) {
        strncpy(word_array[i].word, selected_list[i], WORD_QUERY_MAX_WORD_LEN - 1);
        word_array[i].word[WORD_QUERY_MAX_WORD_LEN - 1] = '\0'; // 确保字符串结尾
    }

    return word_array;
}

//搜索单词列表
word_query_word_entry_t *word_query_search_word_list(char *word, int *size,int max_size) {
    static int count = 0;
    char *word_list_label_text1[] = {"apple 苹果","speak 说（某种语言）","surprise 惊奇","hungry 饥饿的",
                                     "thirsty 口渴的"};
//    char *word_list_label_text1[] = {"apple 苹果"};
//    char *word_list_label_text2[] = {"apple 苹果","speak 说（某种语言）","surprise 惊奇","hungry 饥饿的",
//                                     "sun 太阳"};
    char *word_list_label_text2[] = {"apple 苹果","speak 说（某种语言）","surprise 惊奇","hungry 饥饿的",
                                     "thirsty 口渴的"};
    char **word_list_label_text3 = NULL;


    // 列表数组（指针数组）和列表大小
//    char **word_lists[] = {word_list_label_text1, word_list_label_text2, word_list_label_text3};
    char **word_lists[] = {word_list_label_text1, word_list_label_text2};
    int list_sizes[] = {
            sizeof(word_list_label_text1) / sizeof(char *),
            sizeof(word_list_label_text2) / sizeof(char *),
//            0  // 这里暂时设为 0，后续检查是否 word_list_label_text3 为空
    };
    int list_count = sizeof(word_lists) / sizeof(word_lists[0]); // 总共有几个列表
    int index = count % list_count;  // 轮询索引

    char **selected_list = word_lists[index];
    *size = list_sizes[index];
    count++; // 更新 count，保证下次切换

    // 如果选中的列表为空,判断是否带键盘，如果不带键盘直接返回Null
    if (selected_list == NULL || *size == 0) {
        if(ui_word_query_data.is_with_keyboard){
            //当前词库，暂未收录该单词 请重新输入，查找其他单词
            char *word_list_label_text4[] = {"当前词库，暂未收录该单词","请重新输入，查找其他单词"};
            *size = sizeof(word_list_label_text4) / sizeof(char *);
            selected_list = word_list_label_text4;
        }else{
            return NULL;
        }
    }

    if(*size > max_size){
        *size = max_size;
    }

    // 分配结构体数组（连续存储）
    word_query_word_entry_t *word_array = (word_query_word_entry_t *)malloc((*size) * sizeof(word_query_word_entry_t));
    if (!word_array) {
        *size = 0;
        return NULL; // 内存分配失败
    }

    // 逐个复制单词
    for (int i = 0; i < *size; ++i) {
        strncpy(word_array[i].word, selected_list[i], WORD_QUERY_MAX_WORD_LEN - 1);
        word_array[i].word[WORD_QUERY_MAX_WORD_LEN - 1] = '\0'; // 确保字符串结尾
    }

    return word_array;
}




